using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections.Generic;
using System.Windows.Forms;
using Clue_Less_Client.localhost;
using System.Text;

namespace Clue_Less_Client
{
    /// <summary>
    /// Class representing game board
    /// </summary>
    public class Board
    {
        private List<BoardCell> cells = new List<BoardCell>();
        private MainForm frm;

        public BoardCell GetCell(short x, short y)
        {
            foreach (BoardCell cell in cells)
            {
                if (cell.XPosition == x &&
                    cell.YPosition == y)
                    return cell;
            }
            return null;
        }

        public BoardCell GetCell(Rooms room)
        {
            foreach (BoardCell cell in cells)
            {
                if (cell.Room == room)
                    return cell;
            }
            return null;
        }

        public void Draw()
        {
            foreach (BoardCell cell in cells) 
            {
                Control ctrl = frm.GetControlByName(frm, "pbCell" + cell.XPosition + "" + cell.YPosition);
                if (ctrl != null)
                {
                    PictureBox box = (PictureBox)ctrl;
                    DrawCell(cell, box, false);
                }
            }
        }

        public void DrawCell(BoardCell cell, PictureBox box, bool isHover)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(cell.Room.ToString());
            if (isHover)
                box.Image = cell.SmallImage;
            else
                box.Image = cell.DisabledImage;
            
            box.Tag = cell;

            int count = 0;
            foreach (Character character in frm.Characters)
            {
                if (character.XPosition == cell.XPosition &&
                    character.YPosition == cell.YPosition)
                {
                    count++;
                }
            }

            int index = 0;
            foreach (Character character in frm.Characters)
            {
                if (character.XPosition == cell.XPosition &&
                    character.YPosition == cell.YPosition)
                {
                    Bitmap enabledRoom = null;
                    Bitmap disabledRoom = null;
                    if (count == 1)
                    {
                        enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.SmallImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                        disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.DisabledImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                        sb.Append(" with " + character.CharacterType.ToString());
                    }
                    else if (count == 2)
                    {
                        if (index == 0)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.SmallImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.DisabledImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            sb.Append(" with " + character.CharacterType.ToString());
                        } else {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            sb.Append(" and " + character.CharacterType.ToString());
                        }
                    }
                    else if (count == 3)
                    {
                        if (index == 0)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.SmallImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(cell.DisabledImage), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            sb.Append(" with " + character.CharacterType.ToString());
                        }
                        else if (index == 1)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                            sb.Append(", " + character.CharacterType.ToString());
                        }
                        else
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            sb.Append(" and " + character.CharacterType.ToString());
                        }
                    }
                    else if (count == 4)
                    {
                        if (index == 0)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(cell.SmallImage, Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            disabledRoom = BitmapManipulator.OverlayBitmap(cell.DisabledImage, Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopLeft);
                            sb.Append(" with " + character.CharacterType.ToString());
                        }
                        else if (index == 1)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopRight);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.TopRight);
                            sb.Append(", " + character.CharacterType.ToString());
                        }
                        else if (index == 2)
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomLeft);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomLeft);
                            sb.Append(", " + character.CharacterType.ToString());
                        }
                        else
                        {
                            enabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            disabledRoom = BitmapManipulator.OverlayBitmap(new Bitmap(box.Image), Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.BottomRight);
                            sb.Append(", and " + character.CharacterType.ToString());
                        }
                    }
                    else
                    {
                        enabledRoom = BitmapManipulator.OverlayBitmap(cell.SmallImage, Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                        disabledRoom = BitmapManipulator.OverlayBitmap(cell.DisabledImage, Images.characterTokens[character.CharacterType], 100, BitmapManipulator.ImageCornerEnum.Center);
                        sb.Append(" with " + character.CharacterType.ToString());
                    }

                    if (isHover)
                        box.Image = enabledRoom;
                    else
                        box.Image = disabledRoom;

                    frm.ToolTip1.SetToolTip(box, sb.ToString());

                    index++;
                }
            }
        }

        public Board(MainForm frm)
        {
            this.frm = frm;
            // Initializing the board
            cells.Add(new BoardCell(Rooms.Study, 0, 0, Properties.Resources.Cell00, Properties.Resources.Cell00dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 0, 1, Properties.Resources.Cell01, Properties.Resources.Cell01dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Library, 0, 2, Properties.Resources.Cell02, Properties.Resources.Cell02dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 0, 3, Properties.Resources.Cell03, Properties.Resources.Cell03dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Conservatory, 0, 4, Properties.Resources.Cell04, Properties.Resources.Cell04dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 1, 0, Properties.Resources.Cell10, Properties.Resources.Cell10dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Empty, 1, 1, Properties.Resources.Cell11, Properties.Resources.Cell11dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 1, 2, Properties.Resources.Cell12, Properties.Resources.Cell12dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Empty, 1, 3, Properties.Resources.Cell13, Properties.Resources.Cell13dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 1, 4, Properties.Resources.Cell14, Properties.Resources.Cell14dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Hall, 2, 0, Properties.Resources.Cell20, Properties.Resources.Cell20dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 2, 1, Properties.Resources.Cell21, Properties.Resources.Cell21dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.BilliardRoom, 2, 2, Properties.Resources.Cell22, Properties.Resources.Cell22dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 2, 3, Properties.Resources.Cell23, Properties.Resources.Cell23dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Ballroom, 2, 4, Properties.Resources.Cell24, Properties.Resources.Cell24dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 3, 0, Properties.Resources.Cell30, Properties.Resources.Cell30dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Empty, 3, 1, Properties.Resources.Cell31, Properties.Resources.Cell31dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 3, 2, Properties.Resources.Cell32, Properties.Resources.Cell32dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Empty, 3, 3, Properties.Resources.Cell33, Properties.Resources.Cell33dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 3, 4, Properties.Resources.Cell34, Properties.Resources.Cell34dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Lounge, 4, 0, Properties.Resources.Cell40, Properties.Resources.Cell40dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 4, 1, Properties.Resources.Cell41, Properties.Resources.Cell41dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.DiningRoom, 4, 2, Properties.Resources.Cell42, Properties.Resources.Cell42dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Corridor, 4, 3, Properties.Resources.Cell43, Properties.Resources.Cell43dis, Properties.Resources.Cell00));
            cells.Add(new BoardCell(Rooms.Kitchen, 4, 4, Properties.Resources.Cell44, Properties.Resources.Cell44dis, Properties.Resources.Cell00));
        }

        public void RefreshChat()
        {
            frm.tbChatWindow.Text = "";
            StringBuilder sb = new StringBuilder();
            foreach (ChatMessage message in frm.Chats)
            {
                sb.AppendLine(message.PlayerName + "[" + message.Time.ToShortTimeString() + "] > " +
                    message.Message);
            }
            frm.tbChatWindow.Text = sb.ToString();
            //frm.tbChatWindow.Select(0, 0);
        }
    }
}
